package com.zone.lib.download;

import android.content.Context;
import android.content.Intent;

import com.zone.lib.download.bean.DownloadInfo;
import com.zone.lib.download.bean.RequestInfo;
import com.zone.lib.download.db.entity.Download;
import com.zone.lib.download.enums.AppActionEnum;
import com.zone.lib.download.service.DownloadService;
import com.zone.lib.download.utils.LogUtils;
import com.zone.sdk.module.appstore.entity.AppDetailEntity;
import com.zone.sdk.module.appstore.entity.AppEntity;

import org.apache.commons.lang3.ObjectUtils;

import java.io.File;
import java.util.ArrayList;

public class DownloadHelper {
    
    public static final String TAG = "DownloadHelper";

    private volatile static DownloadHelper SINGLETANCE;

    private static ArrayList<RequestInfo> requests = new ArrayList<>();

    private DownloadHelper(){
    }

    public static DownloadHelper getInstance(){
        if (SINGLETANCE == null){
            synchronized (DownloadHelper.class){
                if (SINGLETANCE == null){
                    SINGLETANCE = new DownloadHelper();
                }
            }
        }
        return SINGLETANCE;
    }

    /**
     * 提交  下载/暂停  等任务.(提交就意味着开始执行生效)
     * @param context
     */
    public synchronized void submit(Context context){
        if (requests.isEmpty()){
            LogUtils.w("没有下载任务可供执行");
            return;
        }
        Intent intent = new Intent(context, DownloadService.class);
        intent.putExtra(DownloadService.SERVICE_INTENT_EXTRA, requests);
        context.startService(intent);
        requests.clear();
    }// end of "submit(..."

    /**
     *  添加 新的下载任务
     *
     * @param code  code
     * @param file  存储在某个位置上的文件
     * @param action  下载过程会发出广播信息.该参数是广播的action
     * @return   DownloadHelper自身 (方便链式调用)
     */
    public DownloadHelper addTask(String code, File file, int action, long size, String version, String vin){
        DownloadInfo downloadInfo = new DownloadInfo();
        downloadInfo.setCode(code);
        downloadInfo.setAction(action);
        downloadInfo.setFile(file);
        downloadInfo.setSize(size);
        downloadInfo.setVin(vin);
        downloadInfo.setVersion(version);
        RequestInfo requestInfo = createRequest(downloadInfo);
        requests.add(requestInfo);
        return this;
    }

    /**
     *  添加 新的下载任务
     *
     */
    public DownloadHelper addTask(File file, String vin, int status, AppDetailEntity appEntity, Download download){
        return addTask(null, file, vin, status, appEntity, download);
    }

    /**
     *  添加 新的下载任务
     *
     */
    public DownloadHelper addTask(String userId, File file, String vin, int status, AppDetailEntity appEntity, Download download){
        DownloadInfo downloadInfo = new DownloadInfo();
        downloadInfo.setAppEntity(appEntity);
        downloadInfo.setDownload(download);
        downloadInfo.setCode(appEntity.getCode());
        downloadInfo.setAction(status);
        downloadInfo.setFile(file);
        downloadInfo.setUserId(userId);
//        downloadInfo.setSize(Long.parseLong(appEntity.getSize()));
        downloadInfo.setSize(download.getSize());
        downloadInfo.setVin(vin);
        downloadInfo.setVersion(download.getVersion());
        RequestInfo requestInfo = createRequest(downloadInfo);
        requests.add(requestInfo);
        return this;
    }

    public DownloadHelper refreshTask(Context context){
        Intent intent = new Intent(context, DownloadService.class);
        intent.putExtra(DownloadService.SERVICE_REFRESH_TASK, "1");
        context.startService(intent);
        return this;
    }

    public DownloadHelper deleteTask(Context context, String code){
        Intent intent = new Intent(context, DownloadService.class);
        intent.putExtra(DownloadService.SERVICE_DELETE_TASK, code);
        context.startService(intent);
        return this;
    }

    public DownloadHelper pauseAllTask(Context context){
        Intent intent = new Intent(context, DownloadService.class);
        intent.putExtra(DownloadService.SERVICE_PAUSE_ALL_TASK, "");
        context.startService(intent);
        return this;
    }

    /**
     * 设定该模块是否输出 debug信息
     * Todo 要重构log模块, 对于我们的静态内部类，目前还不生效
     */
    private DownloadHelper setDebug(boolean isDebug){
        LogUtils.setDebug(isDebug);
        return this;
    }

    private RequestInfo createRequest(DownloadInfo downloadInfo){
        if (ObjectUtils.isNotEmpty(downloadInfo)){
            RequestInfo request = new RequestInfo();
            request.setDownloadInfo(downloadInfo);
            return request;
        }
        return null;
    }

    private RequestInfo createRequest(String code, String url, File file, int action, int dictate, String downloadUrl, long size, String version){
        RequestInfo request = new RequestInfo();
        request.setDictate(dictate);
        request.setDownloadInfo(new DownloadInfo(code, url, file, action, downloadUrl, size, version));
        return request;
    }
}
